﻿Shader "Screen/Light Sprite Overlay"
{
    Properties
    {
        [HideInInspector]_MainTex ("Texture", 2D) = "white" {}
        [HideInInspector]_BGCamRT("BG Render Texture",2D) = "black" {}
        [HideInInspector]_InnerWallCamRT("Inner Wall Render Texture",2D) = "black" {}
        [HideInInspector]_PropCamRT("Prop Render Texture",2D) = "black" {}
        [HideInInspector]_LightCamRT("Lighting Render Texture",2D) = "black" {}
        [HideInInspector]_PlayerCamRT("Player Render Texture",2D) = "black" {}
        [HideInInspector]_RelayCamRT("Relay Render Texture",2D) = "black" {}
        [HideInInspector]_EdgeCamRT("Edge Render Texture",2D) = "black" {}
        _lightStrength("Light Strength",Range(0,5)) = 0.6
        _ShadowStrength("Main Shadow Strength",Range(0,1)) = 0.34
        
    }
    SubShader
    {
        Tags{"Queue" = "Transparent"}
        Cull Off ZWrite Off ZTest Always
        Blend SrcAlpha OneMinusSrcAlpha
        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            // make fog work
            #pragma multi_compile_fog

            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                //UNITY_FOG_COORDS(1)
                float4 vertex : SV_POSITION;
            };

            sampler2D _MainTex;
            float4 _MainTex_ST;

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = v.uv;
                //UNITY_TRANSFER_FOG(o,o.vertex);
                return o;
            }

            sampler2D _LightCamRT,_EdgeCamRT,_RelayCamRT,_BGCamRT,_PlayerCamRT,_PropCamRT,_InnerWallCamRT;
            //sampler2D _BGCamRT;
            float _ShadowStrength;
            //float _EdgeShadowStrength;
            float _lightStrength;

            fixed4 AlphaBlend(fixed4 src,fixed4 dst){
                fixed4 result = fixed4(0,0,0,0);
                result.rgb = src.rgb * src.a + dst.rgb * (1 - src.a);
                result.a = 1;
                return result;

            }
            fixed4 frag (v2f i) : SV_Target
            {
                // sample the textures
                fixed4 col = tex2D(_MainTex, i.uv);
                fixed4 innerWall = tex2D(_InnerWallCamRT,i.uv);
                fixed4 bg = tex2D(_BGCamRT,i.uv);
                fixed4 prop = tex2D(_PropCamRT,i.uv);
                fixed4 player = tex2D(_PlayerCamRT,i.uv);
                fixed4 relay = tex2D(_RelayCamRT,i.uv);
                fixed4 edge = tex2D(_EdgeCamRT,i.uv);
                fixed4 lights = tex2D(_LightCamRT,i.uv);

                //Apply light
                innerWall.rgb = lerp(innerWall.rgb * (1 - _ShadowStrength),innerWall.rgb,lights * _lightStrength);

                //relay.rgb = lerp(relay.rgb * (1 - _ShadowStrength),relay.rgb,lights * _lightStrength);

                //Blend
                col = AlphaBlend(bg,col);
                col = AlphaBlend(innerWall,col);
                col = AlphaBlend(prop,col);
                col = AlphaBlend(player,col);
                col = AlphaBlend(relay,col);
                col = AlphaBlend(edge,col);
                //add edge
                return col;
            }
            ENDCG
        }
    }
}
